package com.czmh.etc.onlinepubsys.utils.mingchuang;

import com.czmh.etc.onlinepubsys.entity.mingchuang.CarInfo;

public abstract class OBUDataEncUtil {

    public static String dataEnc2(String beginDate, String endDate) {
        StringBuilder sb = new StringBuilder("04D681120D");
        sb.append(beginDate).append(endDate).append("01");
        return sb.toString();
    }

    public static String dataEnc1(CarInfo carInfo) {
        StringBuilder sb = new StringBuilder(128);
        sb.append("04D681003F");
        try {
            //车牌号
            int i = 0;
            byte[] bs = carInfo.getVehPlate().getBytes("GB2312");
            for (byte b : bs) {
                if (b < 0) {
                    sb.append(Integer.toHexString(b).substring(6, 8));
                } else {
                    sb.append(Integer.toHexString(b));
                }
                i += 2;
            }
            for (; i < 24; i++) {
                sb.append("0");
            }
            //车牌色
            sb.append("00");
            Integer vehColor = carInfo.getVehColor();
            if (vehColor < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(vehColor));

            //收费车型
            Integer type = carInfo.getType();
            if (type < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(type));

            //车辆用户类型
            sb.append("00");

            //车辆尺寸
            int length = carInfo.getVehLength() / 100;
            int width = carInfo.getVehWidth() / 100;
            int height = carInfo.getVehHeight() / 100;

            String hex = Integer.toHexString(length);
            int len = hex.length();
            for (i = 0; i < 4 - len; i++) {
                sb.append("0");
            }
            sb.append(hex);

            if (width < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(width));

            if (height < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(height));

            //车轮数
            Integer wheelCount = carInfo.getWheelCount();
            if (wheelCount < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(4));

            //车轴数
            Integer axleCount = carInfo.getAxleCount();
            if (axleCount < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(2));

            //轴距
            hex = Integer.toHexString(carInfo.getAxleDistance());
            len = hex.length();
            for (i = 0; i < 4 - len; i++) {
                sb.append("0");
            }
            sb.append(hex);

            //货车车辆载重/ 客车座位数，其中，载重的单位为：kg
            if (carInfo.getType() == 1) {
                //客车座位数
                hex = Integer.toHexString(carInfo.getApprovedCount());
                len = hex.length();
                for (i = 0; i < 6 - len; i++) {
                    sb.append("0");
                }
                sb.append(hex);
            } else if (carInfo.getType() == 11) {
                //货车车辆载重
                hex = Integer.toHexString(carInfo.getPermittedWeight());
                len = hex.length();
                for (i = 0; i < 6 - len; i++) {
                    sb.append("0");
                }
                sb.append(hex);
            }

            //车辆特征描述
            for (i = 0; i < 32; i++) {
                sb.append("F");
            }

            //车辆发动机号
            i = 0;
            bs = carInfo.getEngineNum().getBytes("GB2312");
            for (byte b : bs) {
                sb.append(Integer.toHexString(b));
                i += 2;
            }
            for (; i < 32; i++) {
                sb.append("0");
            }

            return sb.toString().toUpperCase();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
